package exe.jvmbytecodes;
import java.io.IOException;

import java.io.*;
import java.util.*;
import java.net.*;

import org.jdom.*;

import exe.*;
import exe.pseudocode.*;




 // Recognizes all byte codes that contain if
 // only if_cmpgt
 
public class Bytecode_if extends Bytecode_ {
    int counter = 0;

	Bytecode_if(String str) 
	{
		parse(str);
	}

	public int execute(Frame_ f) throws IOException 
	{
		next = lineNumber + 1;
		next = next + 2;
		//If
		Integer x, y;
		Random rand = new Random();
		int random = rand.nextInt(2);
		//if_cmpgt

		if(arguments.get(0).contains("icmp"))
		{
			System.out.println("Enter if_icmp");	
			if(arguments.get(0).contains("icmpgt"))
			{				
				System.out.println("Enter if_icmpgt");		
				x = (Integer) f._stack.pop();

				System.out.println(f._stack);
				y = (Integer) f._stack.pop();

				if (counter == 0) {
				    createQuestion1(x, y, f);
				    counter++;
				}else
				    createQuestion2(x, y, f);

				/*
				if(random == 1)
				{
					createQuestion1(x, y, f);
				}				}
				else
				{
					createQuestion2(x, y, f);
				}
				*/
				questionID++;
				if( x > y-1)
				    {   /*no jump*/  }
				else
				    next = Integer.parseInt(arguments.get(1));
				f.stack.set("",f.currentStackHeight++);
				f.stack.set("",f.currentStackHeight++);
			}		
		}
		return next;
	}

	void createQuestion1(int x, int y, Frame_ f) throws IOException
	{
		XMLtfQuestion question = new XMLtfQuestion(show, questionID + "");
		question.setQuestionText("The bytecode will jump to line number " + arguments.get(1) + ".");
		question.setAnswer(x<=y);
		f.stack.setColor(f.currentStackHeight,"#CD0000");
		f.stack.setColor(f.currentStackHeight+1,"#CD0000");
		show.writeSnap(TITLE, muri.doc_uri(lineNumber), muri.make_uri(lineNumber, pseudo.RED), question, runTimeStack , f.stack,  heap, f.localVariableArray);
		f.stack.setColor(f.currentStackHeight,"#999999");
		f.stack.setColor(f.currentStackHeight+1,"#999999");
	}

	void createQuestion2(int x, int y,  Frame_ f) throws IOException
	{
		XMLmcQuestion question = new XMLmcQuestion(show, questionID + "");
		question.setQuestionText("What line number will the program jump to next?");
		question.addChoice(next + "");
		question.addChoice(arguments.get(1));
		question.addChoice(lineNumber + "");
			if(x > y)
				question.setAnswer(1);
			else
				question.setAnswer(2);
		f.stack.setColor(f.currentStackHeight,"#CD0000");
		f.stack.setColor(f.currentStackHeight+1,"#CD0000");
		show.writeSnap(TITLE, muri.doc_uri(lineNumber), muri.make_uri(lineNumber, pseudo.RED), question, runTimeStack , f.stack,  heap, f.localVariableArray);
		f.stack.setColor(f.currentStackHeight,"#999999");
		f.stack.setColor(f.currentStackHeight+1,"#999999");
	}

	void createQuestion3( Frame_ f) throws IOException
	{
		XMLmcQuestion question = new XMLmcQuestion(show, questionID + "");
		question.setQuestionText("What java code does " + opcode + underscore + arguments.get(0) + " imply?");
		question.addChoice("if(value 1 = value 2)");
		question.addChoice("if(value 1 <= value 2");
		question.addChoice("if(value 1 < value 2");
		question.addChoice("if(value 1 > value 2");
		question.addChoice("if(value 1 >= value 2");

		question.setAnswer(3);
		show.writeSnap(TITLE,muri.doc_uri(lineNumber),muri.make_uri(lineNumber, pseudo.RED), question, runTimeStack , f.stack,  heap, f.localVariableArray);
	}

}
